<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="resetFormData"
      >
        创建用户
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      row-key="id"
      border
    >
      <el-table-column prop="account" label="账号" />
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="createTime" label="创建时间" />

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="{ row }">
          <el-button type="success" @click="showRoleList(row)"
            >配置角色
          </el-button>
          <el-button @click="resetPass(row)" type="warning" size="mini"
            >重置密码</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      style="width: 1100px;margin: auto"
    >
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="fromTemp"
        label-position="left"
        label-width="90px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="账号" prop="account">
          <el-input v-model="fromTemp.account" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="fromTemp.name" />
        </el-form-item>

        <el-form-item label="密码" prop="name">
          <el-input v-model="fromTemp.password" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-right: 50px">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="createUser">
          确认
        </el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="配置角色"
      :visible.sync="dialogButtonFormVisible"
      style="margin: auto"
    >
      <SystemRoleList :key="updateList" :userid="userId" />
    </el-dialog>
  </div>
</template>
<script>
import {
  GetSystemUserList,
  CreateUser,
  resetPass
} from "@/api/SystemManagerApi";
import SystemRoleList from "@/views/SystemSetting/SystemUser/components/SystemRoleList";
export default {
  components: { SystemRoleList },
  data() {
    return {
      id: "000000",
      rules: {
        account: [
          { required: true, message: "type is required", trigger: "change" }
        ],
        name: [
          { required: true, message: "type is required", trigger: "change" }
        ]
      },
      title: "编辑账号",
      fromTemp: {
        account: null,
        name: "",
        password: ""
      },
      dialogFormVisible: false,
      dialogButtonFormVisible: false,
      listLoading: false,
      list: [],
      userId: null,
      updateList: null
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      GetSystemUserList().then(response => {
        this.list = response.data;
        this.listLoading = false;
      });
    },
    resetFormData() {
      this.fromTemp = {
        account: null,
        name: ""
      };
      this.dialogFormVisible = true;
    },
    createUser() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          CreateUser(this.fromTemp).then(response => {
            this.dialogFormVisible = false;
            this.$notify({
              title: "Success",
              message: "Created Successfully",
              type: "success",
              duration: 2000
            });
            this.getList();
          });
        }
      });
    },
    showRoleList(row) {
      this.userId = row.id;
      this.updateList = this.uuid();
      this.$nextTick(() => {
        this.dialogButtonFormVisible = true;
      });
    },
    uuid() {
      function S4() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
      }
      return (
        S4() +
        S4() +
        "-" +
        S4() +
        "-" +
        S4() +
        "-" +
        S4() +
        "-" +
        S4() +
        S4() +
        S4()
      );
    },
    // 重置密码
    resetPass(row) {
      resetPass(row.account).then(res => {
        if (res.code === 20000) {
          this.$message.success("重置密码成功...");
        }
      });
    }
  }
};
</script>
